/*
 * Alexey Danchuk
 * This software is an implementation of the task from OOP module.
 */
package knowledgetestsystem;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Scanner;

/**
 * This class describes input & output class
 * 
 * @version 2.03 10 February 2013
 * @author Alexey Danchuk
 */
class InOut {

	static int feedback = 0;

	static String input() throws Exception {
		System.out.println(" input: ");
		BufferedReader inputStream = new BufferedReader(new InputStreamReader(
															System.in));
		String inputed = inputStream.readLine();
		return inputed;
	}

	String inputQuestion() throws Exception {
		System.out.print("Question for");
		String inQuestion = input();
		return inQuestion;
	}

	/**
	 * Method to input variants. Format: variant1 variant2...
	 * 
	 * @return answers - variants
	 * @throws Exception
	 */
	String[] inputAnswers() throws Exception {
		String[] answers = null;
		int index = 0;
		System.out.print("Answers for");
		String inAnswer = input();
		int size = Manipulator.sizeDetect(inAnswer);
		answers = new String[size];
		Scanner scanInput = new Scanner(inAnswer);
		while (scanInput.hasNext()) {
			answers[index] = scanInput.next();
			index++;
		}
		scanInput.close();
		return answers;
	}

	String[] inputCorrectAnswer() throws Exception {
		String[] correctAnswers = null;
		int index = 0;
		System.out.print("Correct answer(s) for");
		String inAnswers = input();
		int size = Manipulator.sizeDetect(inAnswers);
		if (size > Manipulator.SIMPLE_ANSWER_FLAG) {
			correctAnswers = new String[size];
			Scanner scanCorrect = new Scanner(inAnswers);
			while (scanCorrect.hasNext()) {
				correctAnswers[index] = scanCorrect.next();
				index++;
			}
			scanCorrect.close();
			return correctAnswers;
		} else {
			correctAnswers = new String[Manipulator.SIMPLE_ANSWER_FLAG];
			correctAnswers[0] = inAnswers;
			return correctAnswers;
		}
	}

	String[] inputUserAnswers() throws Exception {
		String[] userAnswers = inputAnswers();
		return userAnswers;
	}

	static void outputQuestions(Question[] systemQuestions) {
		System.out.println("Question is: ");
		for (int i = 0; i < systemQuestions.length; i++) {
			System.out.println(systemQuestions[i].outQuestionText());
			outputAnswersVariants(systemQuestions[i]);
			try {
				InOut input = new InOut();
				String[] userAnswers = input.inputUserAnswers();
				feedback += systemQuestions[i].chekAnswer(userAnswers);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

	static void outputAnswersVariants(final Question actualQuestion) {
		String[] choices = actualQuestion.getAnswerChoices();
		for (String choice : choices) {
			System.out.println("Variant: " + choice);
		}
	}
}
